<?php
  
  class Cache{
  	

	/*
	 * 更新首页缓存
	 */
	static public function updateIndex(){  
       	$block = DB::fetch_array("select *from `bb_template_group` as TG left join `bb_block` as B on TG.`groupid`=B.`groupid` left join `bb_block_template` as BT on B.`templateid`=BT.`templateid` where TG.`isused`='1'");
	   	$succeed = true;
		foreach($block as $val){
			$type = $val[2];
			File::deleteFile("./Html/{$type}", "delete files");  //先删除文件夹中的所有文件
			$cssCon  =$val['css']?"<style type=\"text/css\">\n" .$val['css']."\n</style>\n":"";
			$jsCon   =$val['js']?"<script language=\"Javascript\" type=\"text/javascript\">\n" .$val['js']."\n</javascript>\n":"";
			$htmlCon =$val['content'];
			$con = $htmlCon.$cssCon.$jsCon;
			$fileName = "Html/{$type}/index".$val['terminalid'].".html";
			if(!File::createText($fileName,$con)){
				$succeed = false;
				break;
			}
		}
		return $succeed;
       }
	/*
	 * 清空编译文件
	 */
	static public function clearCompiled(){
		File::deleteFile("./BlockCompiled","delete files");  //清空编译文件夹
	}
	
	/*
	 * 设置默认模版组时更新缓存
	 * $groupid：模版组id(关联bb_template_group的groupid)
	 * $type   : 组类型      (关联bb_template_group的type)
	 */
    static public function updateDefaultCash($groupid,$type){  
       	$block = DB::fetch_array("select *from `bb_block` as B left join `bb_block_template` as BT on B.`templateid`=BT.`templateid` where `groupid`='{$groupid}'");
        File::deleteFile("./Html/{$type}", "delete files");  //先删除文件夹中的所有文件
        File::deleteFile("./BlockCompiled","delete files");  //清空编译文件夹
       	$succeed = true;
		foreach($block as $val){
			$cssCon  =$val['css']?"<style type=\"text/css\">\n" .$val['css']."\n</style>\n":"";
			$jsCon   =$val['js']?"<script language=\"Javascript\" type=\"text/javascript\">\n" .$val['js']."\n</javascript>\n":"";
			$htmlCon =$val['content'];
			$con = $htmlCon.$cssCon.$jsCon;
			$fileName = "Html/{$type}/index".$val['terminalid'].".html";
			if(!File::createText($fileName,$con)){
				$succeed = false;
				break;
			}
		}
		return $succeed;
       }
	/*
	 * 更新栏目缓存
	 */ 
	static public function updateCategoryCache($type,$groupid){
		$succeed = true;
		$groupidType = "";
		switch(strtolower($type)){
			case "headergroup":
			              $groupidType="header_groupid";
			              $groupType="HeaderGroup";
			              break;
			case "footergroup":
			             $groupidType="footer_groupid";
				         $groupType="FooterGroup";
			             break;
			case "listgroup":
			            $groupidType="list_groupid";
				        $groupType="FooterGroup";
			            break;
			case "infogroup":
			            $groupidType="info_groupid";
				        $groupType="InfoGroup";
			            break;
			case "categorygroup":
			            $groupidType="category_groupid";
				        $groupType="CategoryGroup";
			            break;
		}
		$categorys = DB::fetch_array("select *from `bb_category` as C left join `bb_category_setting` as CS on C.`categoryid`=CS.`categoryid`
		                           left join `bb_block` as B on CS.`{$groupidType}`=B.`groupid` 
		                           left join `bb_block_template` as BT on B.`templateid`=BT.`templateid` where CS.`{$groupidType}`='{$groupid}'");
		File::deleteFile("./BlockCompiled","delete files");  //清空编译文件夹
		foreach($categorys as $val){
			$groupPath = "./Html/".$val['path']."/{$groupType}";
			File::deleteFile($groupPath , "delete files");  //先删除文件夹中的所有文件
			$cssCon  =$val['css']?"<style type=\"text/css\">\n" .$val['css']."\n</style>\n":"";
			$jsCon   =$val['js']?"<script language=\"Javascript\" type=\"text/javascript\">\n" .$val['js']."\n</javascript>\n":"";
			$htmlCon =$val['content'];
			$con = $htmlCon.$cssCon.$jsCon;
			$fileName = $groupPath."/index".$val['terminalid'].".html";
			if(!File::createText($fileName,$con)){
				$succeed = false;
				break;
			}
		}
        return $succeed;
	}

  }



?>